package com.xbongbong.helper;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.enums.CommandType;
import com.xbongbong.enums.FailureStrategy;
import com.xbongbong.enums.Priority;
import com.xbongbong.enums.ReleaseState;
import com.xbongbong.enums.RunMode;
import com.xbongbong.enums.Status;
import com.xbongbong.enums.TaskDependType;
import com.xbongbong.enums.WarningType;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.util.HeaderUtil;
import com.xbongbong.util.HttpRequestUtils;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 流程启动等操作
 *
 * @ClassName ExecutorProcessHelper
 * @Author 梁鲁江
 * 创建时间 2022/6/21 15:54
 * 修改时间  2022/6/21 15:54
 **/
@Component
public class ExecutorProcessHelper {
    private  String startProcessInstance(
                                        long projectCode,
                                        long processDefinitionCode,
                                        String scheduleTime,
                                        FailureStrategy failureStrategy,
                                        String startNodeList,
                                        TaskDependType taskDependType,
                                        CommandType execType,
                                        WarningType warningType,
                                        int warningGroupId,
                                        RunMode runMode,
                                        Priority processInstancePriority,
                                        String workerGroup,
                                        Long environmentCode,
                                        Integer timeout,
                                        String startParams,
                                        Integer expectedParallelismNumber,
                                        int dryRun){

        String url = HeaderUtil.getHosts()+"/dolphinscheduler/projects/" +
                projectCode +
                "/executors/start-process-instance";
        JSONObject requestData = new JSONObject();
        requestData.put("failureStrategy",failureStrategy.getUpDescp() );
        requestData.put("processDefinitionCode",processDefinitionCode);
        requestData.put("processInstancePriority",processInstancePriority.getUpDescp());
        requestData.put("scheduleTime",scheduleTime);
        requestData.put("warningGroupId",warningGroupId);
        requestData.put("warningType",warningType.getUpDescp());
        requestData.put("dryRun",dryRun);
        requestData.put("environmentCode",environmentCode);
        requestData.put("execType",execType.getUpDescp());
        requestData.put("expectedParallelismNumber",expectedParallelismNumber);
        requestData.put("runMode",runMode.getUpDescp());
        requestData.put("startNodeList",startNodeList);
        requestData.put("startParams",startParams);
        requestData.put("taskDependType",taskDependType.getUpDescp());
        requestData.put("timeout",timeout);
        requestData.put("workerGroup",workerGroup);
        String ret = HttpRequestUtils.post(url, requestData);
        //{"code":0,"msg":"success","data":null,"failed":false,"success":true}
        JSONObject result = JSON.parseObject(ret);
        Integer code = result.getInteger("code");
        if(Objects.equals(code, Status.SUCCESS.getCode())){

        }else {
            
        }
        System.out.println(ret);
        return "";
    }

    /**
     * 启动流程    可用于单次执行，手动刷新操作
     * @param projectCode
     * @param processDefinitionCode
     * @param scheduleTime
     * @param failureStrategyCode
     * @param startNodeList
     * @param taskDependTypeCode
     * @param execTypeCode
     * @param warningTypeCode
     * @param warningGroupId
     * @param runModeCode
     * @param processInstancePriorityCode
     * @param workerGroup
     * @param environmentCode
     * @param timeout
     * @param startParams
     * @param expectedParallelismNumber
     * @param dryRun
     * @return
     */
    public  String startProcessInstance(
            long projectCode,
            long processDefinitionCode,
            String scheduleTime,
            int failureStrategyCode,
            String startNodeList,
            int taskDependTypeCode,
            int execTypeCode,
            int warningTypeCode,
            int warningGroupId,
            int runModeCode,
            int processInstancePriorityCode,
            String workerGroup,
            Long environmentCode,
            Integer timeout,
            String startParams,
            Integer expectedParallelismNumber,
            int dryRun) {

        FailureStrategy failureStrategy = FailureStrategy.getEnum(failureStrategyCode);
        TaskDependType taskDependType = TaskDependType.getEnum(taskDependTypeCode);
        CommandType execType = CommandType.of(execTypeCode);
        WarningType warningType = WarningType.getEnum(warningTypeCode);
        RunMode runMode = RunMode.getEnumByCode(runModeCode);
        Priority processInstancePriority = Priority.getEnum(processInstancePriorityCode);
        return startProcessInstance(projectCode,
                processDefinitionCode,
                scheduleTime,
                failureStrategy,
                startNodeList,
                taskDependType,
                execType,
                warningType,
                warningGroupId,
                runMode,
                processInstancePriority,
                workerGroup,
                environmentCode,
                timeout,
                startParams,
                expectedParallelismNumber,
                dryRun);
    }

    public  String startProcessInstance(long projectCode, long processDefinitionCode){
       return startProcessInstance(projectCode
                ,processDefinitionCode
                , "1"
                ,1
                ,null
                ,2
                ,0
                ,0
                ,0
                ,0
                ,2
                ,"default"
                ,null
                ,null
                ,null
                ,null
                ,0
        );
    }
        

    
    
    
    public static void main(String[] args) {
        /*startProcessInstance(5959113343520L
                ,5959113474848L
                , "1"
                ,1
                ,null
                ,2
                ,0
                ,0
                ,0
                ,0
                ,2
                ,"default"
                ,null
                ,null
                ,null
                ,null
                ,0
        );*/
        /*startProcessInstance(5925630295072L
                ,5932838024864L
                , "1"
                ,1
                ,""
                ,2
                ,0
                ,0
                ,0
                ,0
                ,2
                ,"default"
                ,0L
                ,1
                ,""
                ,0
                ,0
        );*/
    }
}
